/**
 * 初始化详情对话框
 */
var TeacherInfoDlg = {
    teacherInfoData : {}
};

/**
 * 清除数据
 */
TeacherInfoDlg.clearData = function() {
    this.teacherInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TeacherInfoDlg.set = function(key, val) {
    this.teacherInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TeacherInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭名师对话框
 */
TeacherInfoDlg.close = function() {
    parent.layer.close(window.parent.Teacher.layerIndex);
}

/**
 * 关闭名师课程对话框
 */
TeacherInfoDlg.closeAddRoom = function() {
    parent.layer.close(window.parent.TeacherRoom.layerIndex);
}

/**
 * 收集数据
 */
TeacherInfoDlg.collectData = function() {
    this
    .set('id')
    .set('teacherId')
    .set('teacherName')
    .set('provinceCode')
    .set('cityCode')
    .set('areaCode')
    .set('orgProvinceCode')
    .set('orgProvinceName')
    .set('orgCityCode')
    .set('orgCityName')
    .set('orgAreaCode')
    .set('orgAreaName')
    .set('schoolId')
    .set('schoolName')
    .set('createTime')
    .set('status')
    .set('siteId')
    .set('createUserId')
    .set('updateUserId')
    .set('updateTime')
    .set('beDelete')
    .set('num')
    .set('imageUrl')
    .set('PCavatar')
    .set('H5avatar')
    .set('teacherDesc')
    .set('beTop');
    
}


/**
 * 获取老师
 */
function searchTeacher() {
	//清空原字段
	 $("#teacherId").val("");
     
        var ajax = new $ax(Feng.ctxPath + "/teacher/getTeacher", function (resultData) {
        	jQuery("#teacherList").html("");
        	console.log(resultData);
        	
        	if(resultData instanceof Array){
                for (key in resultData){
                    var userId=resultData[key].userId;
                    var userName=resultData[key].name;
                     jQuery("#teacherList").append('<a href="javascript:void(0)" onclick="check_teacher(this)" class="list-group-item" key="'+userId+'">'+userName+'</a>');
                }
               
			}else{
				var userId=resultData.userId;
                var userName=resultData.name;
                $("#teacherId").val(userId);
                if(userId=='0'){
                	jQuery("#teacherList").html("无老师数据，请核对站点和老师ID");
                }else{
                	jQuery("#teacherList").append('<a href="javascript:void(0)" class="list-group-item" onclick="check_teacher(this)"  key="'+userId+'">'+userName+'</a>');
                }
                 
			}
        	
        }, function (data) {
            jQuery("#teacherList").html("无老师数据，请核对站点和老师ID");
        });
        var accountId = $("#accountIdValue").val();
        if(accountId==""){
        	 Feng.info("老师ID不能为空!");
        }
        
        ajax.set("accountId",$("#accountIdValue").val());
        ajax.start();
};

function check_teacher(that){
	$("#teacherId").val($(that).attr("key"));
}


/**
 * 获取课程
 */
function searchRoom() {
	//清空原字段
	 $("#roomId").val("");
     $("#roomSubject").val("");
     
        var ajax = new $ax(Feng.ctxPath + "/teacher/getRoom", function (resultData) {
        	jQuery("#roomList").html("");
        	
        	if(resultData instanceof Array){
        		if(resultData.length==0){
        			jQuery("#roomList").append('<a href="javascript:void(0)" class="list-group-item" key="">没有找到课程</a>');
        		}else{
        			for (key in resultData){
                        var roomId=resultData[key].roomId;
                        var subject=resultData[key].subject;
                        //$("#number").val(roomId);
                       // $("#roomSubject").val(subject);
                         jQuery("#roomList").append('<a href="javascript:void(0)" onclick="check_room(this)"  class="list-group-item" key="'+roomId+'">'+subject+'</a>');
                    }
        		}
                
               
			}else{
				jQuery("#roomList").append('<a href="javascript:void(0)" class="list-group-item" key="">没有找到课程</a>');
			}
        	
        }, function (data) {
            jQuery("#roomList").html("无课程数据，请核对课程ID和课程名称");
        });
        var number = $("#numberValue").val();
        var roomSubject=$("#roomSubjectValue").val();
        var teacherId = $("#teacherId").val();
        if(number=="" && roomSubject==""){
        	 Feng.info("课程id和课程名称不能同时为空!");
        }
        
        ajax.set("teacherId",teacherId);
        ajax.set("number",number);
        ajax.set("roomSubject",roomSubject);
        ajax.start();
};

function check_room(that){
	$("#roomId").val($(that).attr("key"));
    $("#roomSubject").val($(that).text());
}

/**
 * 提交添加名师
 */
TeacherInfoDlg.addSubmit = function() {
	 this.clearData();
	 this.collectData();
	 
	 var teacherId = $("#teacherId").val();
    if(teacherId==""){
    	 Feng.info("请选择教师!");
    	 return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/teacher/add", function(data){
        Feng.success("添加成功!");
        window.parent.Teacher.table.refresh();
        TeacherInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.teacherInfoData);
    ajax.start();
}

/**
 * 提交添加名师课程
 */
TeacherInfoDlg.addRoomSubmit = function() {
	 var roomId = $("#roomId").val();
     var roomSubject=$("#roomSubject").val();
     var teacherId = $("#teacherId").val();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/teacher/teacherRoomAdd", function(data){
    	Feng.success(data.info);
        window.parent.TeacherRoom.table.refresh();
        TeacherInfoDlg.closeAddRoom();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    
    if(roomId==""){
   	 Feng.info("请选择课程!");
   	 return;
   }
    
    ajax.set("teacherId",teacherId);
    ajax.set("roomId",roomId);
    ajax.set("roomSubject",roomSubject);
    ajax.start();
}

/**
 * 提交修改
 */
TeacherInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/teacher/update", function(data){
        Feng.success("修改成功!");
        window.parent.Teacher.table.refresh();
        TeacherInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.teacherInfoData);
    ajax.start();
}

$(function() {
	
	// 初始化PC上传
    var PCavatarUp = new $WebUpload("PCavatar");
    PCavatarUp.setUploadBarId("progressBar");
    PCavatarUp.init();
    
 // 初始化H5上传
    var H5avatarUp = new $WebUpload("H5avatar");
    H5avatarUp.setUploadBarId("progressBar");
    H5avatarUp.init();
    
    //站点初始化
    var siteId=$("#siteId").val();
    var text="";
    if (siteId == "001") {
        text = "万柏林";
    } else if (siteId == "002") {
        text = "运城";
    }else if (siteId == "003") {
        text = "贵州";
    }else if (siteId == "004") {
        text = "科协";
    }
    $("#siteValue").val(text);

});
